package com.hzhc.controller.cntrmanage;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.dto.UserDto;
import com.hzhc.entity.CntrSize;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.CntrSizeService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.CntrSizeVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 箱尺寸代码 前端控制器
 * </p>
 *
 * @author 功不唐捐
 * @since 2024-05-23
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/cntrSize")
public class CntrSizeController extends BaseController {

    private final CntrSizeService cntrSizeService;

    /**
     * 获取箱尺寸分页列表
     * @param vo 箱尺寸vo
     * @return ResultUtil<List<CntrSize>>
     */
    @RequestMapping("/getCntrSizeList")
    @ResponseBody
    public ResultUtil<List<CntrSizeVo>> getCntrSizeList(@RequestBody CntrSizeVo vo) {
        Page<CntrSizeVo> pagination = new Page<>(vo.getPageIndex(), vo.getPageSize());
        List<CntrSizeVo> cntrSizes = cntrSizeService.getCntrSizeDataByPage(vo, pagination);
        return ResultUtil.build(cntrSizes, pagination.getTotal(),pagination.getPages(), ResultUtilCodeEnum.SUCCESS);
    }


    /**
     * 添加箱尺寸代码
     * @param request  请求
     * @param cntrSize 箱尺寸代码
     * @return ResultUtil<T>
     */
    @PostMapping("/addCntrSize")
    @SysTemLog(operModul = "箱管理-箱尺寸代码",operType = "ADD",operDesc = "箱尺寸代码添加",modular="PC")
    public ResultUtil<T> addCntrSize(HttpServletRequest request, @RequestBody CntrSize cntrSize) {
        UserDto user = TokenUtil.getUser(request);
        cntrSize.setUserId(user.getId());
        cntrSize.setCreateName(user.getRealname());
        cntrSize.setCreateTime(LocalDateTime.now());
        return cntrSizeService.addCntrSize(cntrSize);

    }

    /**
     * 修改箱尺寸代码
     *
     * @param request    请求
     * @param cntrSize   箱尺寸代码
     * @return ResultUtil<T>
     */
    @PostMapping("/updateCntrSize")
    @SysTemLog(operModul = "箱管理-箱尺寸代码",operType = "UPDATE",operDesc = "箱尺寸代码编辑",modular="PC")
    public ResultUtil<T> updateCntrSize(HttpServletRequest request, @RequestBody CntrSize cntrSize) {
        UserDto user = TokenUtil.getUser(request);
        cntrSize.setUpdateUserId(user.getId());
        cntrSize.setUpdateName(user.getRealname());
        cntrSize.setUpdateTime(LocalDateTime.now());
        return cntrSizeService.updateCntrSize(cntrSize);
    }

    /**
     * 删除箱尺寸代码
     * @param id     箱尺寸代码id
     * @return ResultUtil<T>
     */
    @RequestMapping("/deleteCntrSize/{id}")
    @SysTemLog(operModul = "箱管理-箱尺寸代码",operType = "DELETE",operDesc = "箱尺寸代码删除",modular="PC")
    public ResultUtil<T> deleteCntrSize(@PathVariable("id") Long id) {
        return cntrSizeService.deleteCntrSize(id);
    }

    /**
     * 客户唯一校验
     * @param cntrSize 箱尺寸代码
     * @return ResultUtil<T>
     */
    @PostMapping("/checkCntrSize")
    public ResultUtil<T> checkCntrSize(@RequestBody CntrSize cntrSize) {
        ResultUtil<T> tResultUtil = cntrSizeService.checkCntrSize(cntrSize);
        if(tResultUtil == null){
            return ResultUtil.build(ResultUtilCodeEnum.SUCCESS);
        }
        return tResultUtil;
    }

}
